/*getStuden();*/

//所有token
checkToken();

function checkToken() {
	var systoken = sessionStorage.getItem('systoken');
	if(!systoken) {
		alert("请先登录!")
		window.location.href = "../../login.html"
	}
}

//function getStuden() {
//	layui.use(['table', 'laydate', 'laypage', 'layer'], function() {
//		var laypage = layui.laypage,
//			layer = layui.layer;
//		var table = layui.table;
//		var laydate = layui.laydate;
//		var defaultpageSise = 10;
//		window.ins1 = _render(defaultpageSise);
//
//		function _render(pageSize) {
//			table.render({
//				elem: '#stuTest',
//				height: 600,
//				title: '用户数据表',
//				page: true //开启分页
//					,
//				skin: 'row',
//				even: true,
//				autoSort: false,
//				loading: true,
//				totalRow: true,
//				cols: [
//					[ //表头
//						{
//                          field: 'name',
//							title: '姓名',
//
//						}, {
//                          field: 'score',
//							title: '成绩',
//
//						}
//					]
//				],
//				limit: pageSize
//			});
//		}
//	})
//}
getStudentList();
$(".startSearch").click(function() {
	/*getStudentList();*/
	var studentName = $(".studentName").val();
	if(studentName != "") {
		getStudentList();
	} else {
		layer.msg('请填写学员名字');
	}
})
//获取所有学员
function getStudentList() {
	var name = sessionStorage.getItem('name');
	var type = 0;
	var startTime = $(".startTime").val();
	var endTime = $(".endTime").val();
	var courseName=$(".machingName").val();
	var courseType=$(".course-type option:selected").val();
	var controlType = $(".controlType option:selected").val();
	console.log(name);
	layui.use(['table', 'laydate', 'laypage', 'layer'], function() {
		var laypage = layui.laypage,
			layer = layui.layer;
		var table = layui.table;
		var laydate = layui.laydate;
		$('div[title="导出"]').click(function() {
			$(this).attr('lay-event', 'ads')
			alert(1)
		})
		var stamp = new Date().getTime();
		var systoken = sessionStorage.getItem('systoken');
	    var param = {
	    	courseName:courseName,
	    	courseType:courseType,
	    	controlType:controlType,
			name: name,
			type:type,
			stamp: stamp,
			startDate: startTime,
			endDate: endTime,
			version: 0,
			visit_plat: 0,
			visit_type: 0,
			is_used: 0,
		};
		console.log(param)
		param = JSON.stringify(param);
		var url = path + "/grade/getGrade";
		var defaultpageSise = 10;
		window.ins1 = _render(defaultpageSise);

		function _render(pageSize) {
			var width = $(document).width();
			console.log(width)
			table.render({
				elem: '#stuTest',
				height: 600,
				title: '用户数据表',
				method: 'post',
				url: url,
				//			url: path+"/student/getStudentList",
				// 			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
				page: true,
				where: {
					param: param
				},
				headers: {
					Authorization: systoken
				},
				skin: 'row',
				even: true,
				autoSort: false,
				loading: true,
				parseData: function(res) {
					var data = res.rows
				      console.log(data);
					var msg = '';
					if(res.status == '000') {
						msg = res.info;
					} else if(res.rows == '后台数据为空') {
						msg = '数据为空';
					}
					return {
						"code": res.status,
						"msg": msg,
						"count": res.total,
						"data": res.rows
					};
				},
				done: function(res) {
					console.log(res);
					$('#inp').val(pageSize);
					$("#inp").on('blur', function() {
						console.log("input框文本值：" + $("#inp").val());
						var inputVal = $("#inp").val();
						_render(inputVal);
						//getAllTeacher(inputVal);
					});
				},

				totalRow: true,
				limit: 30,
				width: width * 0.9,
				//				toolbar: '.toolbarDemo',
				cols: [
					[
						{
							field:'courseName',
							title:'培训科目',
							align:'center'
						},
						{
							field:'stuName',
							title:'学员姓名',
							align:'center'
						},
						{
							field: 'grade',
							title: '成绩',
							align:'center'
						},
						{
							field: 'examDate',
							title: '日期',
							align:'center'
						}
					]
				],
				
				done: function(res, curr, count) {
					$('thead span').css({
						"font-size": "16px!important"
					});
				},
				page: true
					,
				//			limits: [10, 20, 30,40,50,100],
				limit: pageSize
					,
				loading: true, //请求数据时，是否显示loading
				//			layout: [ 'prev', 'page', 'next', 'limit', 'skip','count'],
			});
		};
		//		table.on('toolbar(stuTest)', function (obj) {
		//			var checkStatus = table.checkStatus(obj.config.id);
		//			if(obj.qrcode){
		//				return "<img src='"+obj.qrcode+"'/>";
		//			}
		//		}

		//工具栏事件
		table.on('toolbar(stuTest)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			console.log(deletData)
			var exportExel = table.checkStatus('stuTest').data;
			switch(obj.event) {
				case 'add':
					layer.msg('添加');
					break;
				case 'update':
					layer.msg('编辑');
					break;
				case 'delete':
					layer.msg('删除');
					break;
				case 'getCheckData':

					var deletData = checkStatus.data;
					console.log(deletData)
					exportExel = checkStatus.data;
					batchPass(deletData);

					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选')
					break;
				case 'LAYTABLE_EXPORT':
					//					console.log(exportExel);
					table.exportFile('stuTest', exportExel, 'xls');
					//					alert(exportExel);
					break;
			};
		});
		//封装查看批量学员函数开始1203修改batchPass
		function batchPass(deletData, index) {
			console.log(deletData)
			var paramCheck = {
				deletData: deletData
			};
			//				console.log(param1);
			var studentIds = [];
			var deletId = paramCheck.deletData;
			if(!deletId.length) {
				alert("请先选择需要查看的学员！");
				return;
			}
			for(var i = 0; i < deletId.length; i++) {
				studentIds.push(deletId[i].studentId);
				//				console.log(studentIds);
				sessionStorage.setItem('studentIds', studentIds);
				console.log(studentIds)
			}
			jumpExerciseNumber('学员分数', 'scoreDetailEcharts.html');
			getStudentList();
		}
		//日期日期选择器
		laydate.render({
			elem: '.createTime',
			type: 'datetime'
		});
		laydate.render({
			elem: '.updateTime',
			type: 'datetime'
		});
		//监听表格行点击
		table.on('tr', function(obj) {
			console.log(obj);

		});

		//监听表格复选框选择
		table.on('checkbox(stuTest)', function(obj) {
			console.log(obj)

		});

		//监听表格单选框选择
		table.on('radio(stuTest)', function(obj) {
			console.log(obj);
			//					alert("我是单选按钮点击才显示的！");

		});
		//监听表格单选框选择
		table.on('rowDouble(stuTest)', function(obj) {
			//					console.log(obj);
			//						console.log("双击单元格我才出现!");

		});

		//监听单元格编辑
		table.on('edit(stuTest)', function(obj) {

			var value = obj.value //得到修改后的值
				,
				xgdata = obj.data //得到所在行所有键值
				,
				id = xgdata.id //得到id
				,
				field = obj.field; //得到字段
			//						双击单元格改变一个值就可以在这里取得,在这里进行与后台交互,修改信息,
			//						console.log(data);

			//编辑单元格
			editStudent();
			$('.studentname').val("");
			//修改成功与后台交互
			function editStudent() {
				// console.log(id);
				var stamp = new Date().getTime();
				var version = 0;
				var visit_plat = 0;
				var visit_type = 0;
				var is_used = 0;
				var systoken = localStorage.getItem('systoken');
				//									console.log(systoken);
				var param = {
					//										obj[field]:value,
					id: id,
					stamp: stamp,
					version: version,
					visit_plat: visit_plat,
					visit_type: visit_type,
					is_used: is_used,
					systoken: systoken,
				}
				var param = Object.assign(xgdata, param1);
				param = JSON.stringify(param);
				console.log(param);
				var url = path + "/student/updateStudent";
				$.ajax({
					url: url,
					type: "post",
					data: {
						param: param
					},
					dataType: "json",
					success: function() {
						//						重新查询数据
						console.log("更新学员成功!")
						getStudentList();

					}

				})
			}

		});

		//监听行工具事件
		table.on('tool(stuTest)', function(obj) {

			var data = obj.data;

			//			console.log(data.qrcode); 

			//						var id = data.id;
			if(obj.event === 'del') {
				layer.confirm('确定删除这一行数据?', function(index) {
					//							这里编写删除的逻辑,下面两个步骤是完成与后台的交互后才执行的
					var id = data.id;
					//					console.log(id)
					obj.del();
					layer.close(index);
					//								删除表格
					deleteStudent();

					function deleteStudent() {
						var systoken = sessionStorage.getItem('systoken');
						var stamp = new Date().getTime();
						var version = 0;
						var visit_plat = 0;
						var visit_type = 0;
						var is_used = 0;
						//									var systoken=localStorage.getItem(systoken);
						//									console.log(systoken);
						var param = {
							id: id,
							systoken: systoken,
							stamp: stamp,
							version: version,
							visit_plat: visit_plat,
							visit_type: visit_type,
							is_used: is_used,
							//										systoken:systoken
						}
						param = JSON.stringify(param);
						var url = path + "/student/deleteStudent"
						$.ajax({
							url: url,
							type: "post",
							data: {
								param: param
							},
							dataType: "json",
							success: function() {
								//						重新查询数据
								console.log("删除成功!")
								getStudentList();
							}

						})
					}

				});
			} else if(obj.event === 'edit') {
				iframeData = data;
				console.log(iframeData);
				editUser(iframeData);
			} else if(obj.event === 'detail') {
				console.log(data.qrcode);
				layer.open({
					type: 2,
					title: '二维码',
					closeBtn: 0,
					shade: [0.001, '#393D49'],
					area: '100px',
					skin: 'layui-layer-nobg', //沒有背景色
					shadeClose: true,
					content: data.qrcode,
					id: id,
				})

			} else if(obj.event === 'studentPassRate') {
				console.log(data)
				var studentIds = data.id;
				var studentName = data.studentName
				sessionStorage.setItem('studentIds', studentIds);
				sessionStorage.setItem('studentName', studentName)
				console.log(studentIds)
				//						alert(studentIds);
				jumpExerciseNumber('学员通过率', 'scoreDetailEcharts.html?id=' + studentIds);

			}
			//监听排序
			table.on('sort(stuTest)', function(obj) {
				//				console.log(this)
				//return;
				layer.msg('已经完成排序！ ' + obj.field + ' ' + obj.type);
				//服务端排序
				table.reload('stuTest', {
					initSort: obj
						//,page: {curr: 1}
						,
					where: {
						key: obj.field
							,
						order: obj.type
					}
				});
			});

			var $ = layui.jquery,
				active = {
					parseTable: function() {
						table.init('parse-table-demo', {
							limit: 3
						});
					},
					add: function() {
						table.addRow('stuTest')
					}
				};
			//table按钮（删除等）
			$('i').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
			$('.layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			})

			// });

			//		export2Excel(table)

		})
	})

}
sessionStorage.removeItem("menu");
sessionStorage.removeItem("curMenu");